package com.example.app.appstart

import android.app.Application
import android.content.Context
import android.view.Gravity
import androidx.appcompat.app.AppCompatDelegate
import cc.taylorzhang.singleclick.SingleClickUtil
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.Utils
import com.example.app.R
import com.example.app.manager.AutoSizeManger
import com.example.app.weight.MyRefreshFooter
import com.lxj.xpopup.XPopup
import com.rousetime.android_startup.AndroidStartup
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.weikaiyun.fragmentation.FragmentationManager

/**
 * CreateBy:Joker
 * CreateTime:2023/4/28 14:26
 * description：
 */
class UIStartup : AndroidStartup<String>() {

    override fun callCreateOnMainThread(): Boolean = true

    override fun waitOnMainThread(): Boolean = true

    override fun create(context: Context): String? {
        //禁用黑暗模式,
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

        if (context is Application) {
            //android常用方法
            Utils.init(context)

            //统一toast配置
            ToastUtils.getDefaultMaker()
                .setGravity(Gravity.CENTER, 0, 0)
                .setTextColor(ColorUtils.getColor(R.color.color_white))
                .setBgResource(R.drawable.shape_r5_black_a35)

            XPopup.setNavigationBarColor(R.color.color_white)
            XPopup.setIsLightNavigationBar(true)

            AutoSizeManger.init(context)

            initSmartRefreshLayout()
        }

        //初始化fragment管理工具
        FragmentationManager.init()

        //单点时间间隔
        SingleClickUtil.singleClickInterval = 500

        return this.javaClass.name
    }

    private fun initSmartRefreshLayout() {
        SmartRefreshLayout.setDefaultRefreshInitializer { context, layout ->
            layout.apply {
                setEnableAutoLoadMore(true)
                setEnableLoadMore(false)
                setEnableRefresh(false)
            }
        }
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            MaterialHeader(context).apply {
                setColorSchemeColors(ColorUtils.getColor(R.color.color_white), ColorUtils.getColor(R.color.color_black))
            }
        }
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout ->
            MyRefreshFooter(context)
        }
    }

}